﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DataAccess {
    public class TransactionsDetailRepository : DataProvider{
        public bool add(int transID, int transportType, float pricePerProduct, String carrierInfo, int addedQuantity) {
            String sql = "INSERT INTO TransactionsDetail(Trans_ID, TransportType, PricePerProduct, AddedDate, CarrierInfo, AddedQuantity) VALUES({0}, {1}, {2}, '{3}', '{4}', {5})";
            sql = string.Format(sql, transID, transportType, pricePerProduct, DateTime.Now, carrierInfo, addedQuantity);
            if (this.executeQuery(sql) > 0)
                return true;
            return false;
        }

        /// <summary>
        /// update transport price
        /// </summary>
        /// <param name="id"></param>
        /// <param name="transportType"></param>
        /// <param name="transportPrice"></param>
        /// <returns></returns>
        public bool update(int id, int transportType, float transportPrice) {
            String sql = String.Format("UPDATE TransactionDetail SET TransportType = {0}, TransportPrice = {1} WHERE ID = {3}", id, transportType, transportPrice);
            if (this.executeQuery(sql) == 1)
                return true;

            return false;
        }

        public DataTable getDetail(int id) {
            DataTable table = new DataTable();

            String sql = "SELECT Transactions.ID, Transactions.TransID, Transactions.NextTransQuanlity, Transactions.MinQuanlity, Transactions.EndDate, Transactions.BeginDate, Products.ProductID, Products.ProductName, Products.Weight, Products.Height, Products.Width, Products.Length, Products.QuantityAtLeast FROM Partners INNER JOIN LoginLog ON Partners.PartnerID = LoginLog.PartnerID INNER JOIN Transactions ON LoginLog.SID = Transactions.SID INNER JOIN  Products ON Transactions.ProductID = Products.ProductID WHERE (Transactions.ID = {0})";
            sql = string.Format(sql, id);
            table = this.executeQueryDataTable(sql);
            return table;
        }

        public DataTable detailList(int transID) {
            String sql = "SELECT * " +
                         " FROM TransactionsDetail " +
                         " WHERE  (Trans_ID = {0})";
            sql = string.Format(sql, transID);

            return this.executeQueryDataTable(sql);
        }
    }
}
